package main

import (
	"log"
	"time"

	"bcb-cc-observ/pkg"
	"bcb-cc-observ/pkg/channel"
	"bcb-cc-observ/pkg/models"

	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/util/gutil"
)

func tmain() {
	file := "./config/cnf/conn.yaml"
	channelid := "chl-021"

	peerUrl := "grpcs://peer0.org1.example.com:7051"
	orgID := "Org1"
	userID := "Admin"

	var net *pkg.S_Network
	net, err := pkg.NewNetwork(file, userID, orgID, peerUrl)
	if err != nil {
		log.Printf("创建sdk客户端失败: %v\n", err)
		return
	}

	client, err := net.GetLedger(channelid, userID, orgID)
	if err != nil {
		g.Log().Errorf("创建sdk客户端失败, %v", err)
		return
	}
	g.Log().Infof("初始化账本, %#v", *client)

	var csi *models.F_CHL_Summary_Info

	csi, err = channel.GetSummaryInfo(client, channelid)
	if err != nil {
		g.Log().Errorf("-X-> 通道概要信息失败: %v", err)
		return
	}
	g.Log().Infof("---> 通道概要信息: %#v", *csi)
	clt := pkg.NewLedger(client)

	// ----------------- 区块信息 ----------------
	blockNo_Start := 9
	blockNo_End := 9
	for i := blockNo_Start; i <= blockNo_End; i++ {
		startTime := time.Now()
		blk, err := clt.GetBlockInfo(uint64(i))
		if err != nil {
			g.Log().Errorf("区块信息失败, %v", err)
			return
		}
		timeSpan := time.Now().Sub(startTime)
		g.Log().Infof("区块信息, 耗时=%dms, \n%v", timeSpan.Milliseconds(), gutil.Export(blk))
	}

}
